<template>
    <div class="mirror-tab1">
         <p class="g-unit">单位: 元</p>
        <div class="tableBox">
            <el-table
                v-loading="tableLoading"
                :data="tableData"
                height="85vh"
                style="width: 100%"
                class="tableTh28"
                border
                stripe
                :span-method="arraySpanMethod"
                ref="table"
            >
                <el-table-column
                    fixed
                    label="实际/计划"
                    prop="indexType"
                    min-width="90"
                ></el-table-column>

                <el-table-column
                    fixed
                    label="正向/负向"
                    prop="indexName"
                    min-width="80"
                ></el-table-column>
                <el-table-column fixed label="项目" prop="itemName">
                </el-table-column>
                <el-table-column
                    :label="item.name"
                    v-for="(item, index) in titles"
                    :key="index"
                >
                    <el-table-column
                        :label="item.props.label1"
                        :prop="item.props.id1"
                        min-width="120"
                    ></el-table-column>
                    <el-table-column
                        :label="item.props.label2"
                        :prop="item.props.id2"
                        min-width="120"
                    ></el-table-column>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
import { mergeSpan } from "@/utils/mixin";
export default {
    name: "incomeMirror-tab1",
    props: ["rowData0", "rawTime"],
    data() {
        return {
            tableData: [],
            tableLoading: true,
            spanArr: null,
            year: "",
            month: "",
            time: {},
            title1: [],
            title2: [],
            title3: [],
            subtype: "0",
            titles: []
        };
    },

    methods: {
        //table合并行
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (
                (columnIndex == 0 && this.subtype == "0") ||
                (columnIndex == 1 && this.subtype == "0")
            ) {
                return this.spanArr && this.spanArr[rowIndex][columnIndex];
            }
        },

        init() {
            let that = this,
                titles = that.rowData0.parentName.slice(3),
                titlesId = that.rowData0.titleId.slice(3),
                titleArr = [],
                titleInfo = [];

            that.tableData = that.rowData0.list;
            titleArr = that.AreaUnique(titles);
            titlesId = that.formatArr(titlesId, 2);

            that.tableLoading = true;
            titleArr.map((it, i) => {
                let obj = {
                    name: it,
                    props: {
                        id1: titlesId[i][0],
                        id2: titlesId[i][1],
                        label1: "当月",
                        label2: "年度累计"
                    }
                };
                titleInfo.push(obj);
            });
            
            that.titles = titleInfo;
            if (that.rowData0 && that.rowData0.list.length) {
                that.spanArr = mergeSpan(that.rowData0.list, [
                    "indexType",
                    "indexName"
                ]);
            }

            that.$nextTick(() => {
                that.tableLoading = false;
                that.$refs.table.doLayout();
            });
        },
        AreaUnique(arr) {
            var arr1 = []; // 新建一个数组来存放arr中的值
            for (var i = 0, len = arr.length; i < len; i++) {
                if (arr1.indexOf(arr[i]) === -1) {
                    arr1.push(arr[i]);
                }
            }
            return arr1;
        },
        formatArr(arr, num) {
            //最后返回的结果为一个二维数组
            var newArr = new Array(Math.ceil(arr.length / 2));
            for (let i = 0; i < newArr.length; i++) {
                newArr[i] = [];
            }
            for (let i = 0; i < arr.length; i++) {
                newArr[parseInt(i / num)][i % num] = arr[i];
            }
            return newArr;
        }
    }
};
</script>

<style lang="scss">
.mirror-tab1 {
    .cell {
        text-align: center;
    }
}
</style>
